<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>
            <el-table :data="rolersList" stripe border>
                <!-- 展开列 -->
                <el-table-column type="expand">
                    <template v-slot="scope">
                        <el-row :class="['bottomborder vcenter',i1===0? 'topborder':'']" v-for="(item1,i1) in scope.row.children" :key="i1">
                            <!-- 渲染一级权限 -->
                            <el-col :span="5">
                                <el-tag closable @close="closeTag()">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 渲染二，三级权限 -->
                            <el-col :span="19">
                                <el-row v-for="(item2,i2) in item1.children" :key="i2" :class="['vcenter',i2===0? '':'topborder']">
                                    <el-col :span="6">
                                        <el-tag closable @close="closeTag()" type="success">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag closable type="warning" v-for="(item3,i3) in item2.children" :key="i3" @close="closeTag()">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <!-- 索引列 -->
                <el-table-column type="index"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作">
                    <template v-slot="scope">
                        <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
                        <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
                        <el-button type="warning" size="mini" icon="el-icon-setting" @click="showSet(scope.row)">分配权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <!-- 分配权限对话框 -->
        <el-dialog
        title="分配权限"
        @close="closeRights"
        :visible.sync="setDialogVisible"
        width="50%">
            <!-- 树形控件 -->
            <el-tree :data="rightsList" show-checkbox :props="treeProps" :node-key="id" default-expand-all :default-checked-keys="defaultKeys" ref="treeRef"></el-tree> 
            <span slot="footer" class="dialog-footer">
                <el-button @click="setDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                rolersList:[
                    {roleName:'主管',roleDesc:'唱跳Rap',children:[
                        {authName:'商品管理',path:'goods',children:[
                            {authName:'商品管理',path:'goods',children:[{authName:'商品管理',path:'goods'},{authName:'商品管理',path:'goods'},{authName:'商品管理',path:'goods'}]},
                            {authName:'商品管理',path:'goods',children:[{authName:'商品管理',path:'goods'},{authName:'商品管理',path:'goods'}]},
                            {authName:'商品管理',path:'goods',children:[{authName:'商品管理',path:'goods'},{authName:'商品管理',path:'goods'},{authName:'商品管理',path:'goods'}]}]},
                        {authName:'订单管理',path:'goods',children:[
                            {authName:'订单管理',path:'goods',children:[{authName:'订单管理',path:'goods'}]},
                            {authName:'订单管理',path:'goods',children:[{authName:'订单管理',path:'goods'},{authName:'订单管理',path:'goods'},{authName:'订单管理',path:'goods'}]},
                            {authName:'订单管理',path:'goods',children:[{authName:'订单管理',path:'goods'},{authName:'订单管理',path:'goods'}]}]},
                        {authName:'权限管理',path:'goods',children:[
                            {authName:'权限管理',path:'goods',children:[{authName:'权限管理',path:'goods'},{authName:'权限管理',path:'goods'},{authName:'权限管理',path:'goods'}]},
                            {authName:'权限管理',path:'goods',children:[{authName:'权限管理',path:'goods'},{authName:'权限管理',path:'goods'},{authName:'权限管理',path:'goods'}]},
                            {authName:'权限管理',path:'goods',children:[{authName:'权限管理',path:'goods'}]}]}
                    ]},
                    {roleName:'Boss',roleDesc:'tf'},
                    {roleName:'总监',roleDesc:'唱'},
                    {roleName:'ceo',roleDesc:'跳'},
                    {roleName:'leader',roleDesc:'Rap'},
                    {roleName:'科长',roleDesc:'唱Rap'},
                    {roleName:'组长',roleDesc:'跳Rap'},
                ],
                //权限对话框显示与隐藏
                setDialogVisible:false,
                //所有权限的列表
                rightsList:[],
                //树形控件的属性绑定对象
                treeProps:{
                    label:'authName',
                    children:'children'
                },
                //tree控件默认选中的项
                defaultKeys:[]
            }
        },
        methods : {
            closeTag(){
                this.$confirm('是否删除此标签?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //axios请求
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });          
                });
            },
            showSet(roler) {
                this.rightsList = this.rolersList[0].children;
                this.setDialogVisible = true;
                getKeys(roler, this.defaultKeys)
                //console.log(this.rightsList)
            },
            getKeys(node, arr) {
                //通过递归获取三级权限的id
                if(!node.children){
                    return arr.push(node.id)
                }
                node.children.forEach(item => {
                  this.getKeys(node, arr)
                })
            },
            closeRights() {
                //关闭权限对话框 清空defaultKeys数组
                this.defaultKeys = [];
            },
            allotRights() {
                const keys = [
                    ...this.$refs.treeRef.getCheckedKeys() ,
                    ...this.$refs.treeRef.getHalfCheckedKeys()
                ]
               console.log(keys)
               const str = keys.join(',')
            }
        }
    }
</script>
<style lang="less" scoped>
.el-tag {
    margin:7px;
}

.topborder {
    border-top:1px solid #eee;
}

.bottomborder {
    border-bottom:1px solid #eee;
}

.vcenter {
    display:flex;
    align-items: center;
}
</style>